package com.sk.cardeal.act.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Display;
import android.widget.FrameLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.act.home.fragment.ChooseBrandFragment;
import com.sk.cardeal.act.home.fragment.ChooseModelFragment;
import com.sk.cardeal.act.home.fragment.ChooseSeriesFragment;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.Brand;
import com.sk.cardeal.entity.Model;
import com.sk.cardeal.entity.ModelListBean;
import com.sk.cardeal.entity.Series;
import com.sk.cardeal.entity.SeriesListBean;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MapKeyComparator;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.view.TitleView;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sk on 2017/12/20.
 */

public class BrandChooseActivity extends BaseActivity implements HandleDataCallBack {
    @BindView(R.id.container)
    FrameLayout container;

    private List<Brand> brandList = new ArrayList<>();
    private List<Series> seriesList = new ArrayList<>();

    ChooseBrandFragment chooseBrandFragment;
    ChooseSeriesFragment chooseSeriesFragment;
    ChooseModelFragment chooseModelFragment;

    private LoadingDialog loadingDialog;
    private String brandUrl = "";


    @Override
    protected int getContentView() {
        return R.layout.activity_choose_brand;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        brandList = getIntent().getParcelableArrayListExtra("data");
        init_fragment();
    }

    private void init_fragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        chooseBrandFragment = (ChooseBrandFragment) fm.findFragmentByTag(ChooseBrandFragment.class.getSimpleName());
        if (chooseBrandFragment == null) {
            chooseBrandFragment = new ChooseBrandFragment();
            chooseBrandFragment.setData(brandList);
            chooseBrandFragment.setMyCallBack(this);
            ft.add(R.id.container, chooseBrandFragment, chooseBrandFragment.getClass().getSimpleName());
        }

        chooseSeriesFragment = (ChooseSeriesFragment) fm.findFragmentByTag(ChooseSeriesFragment.class.getSimpleName());
        if (chooseSeriesFragment == null) {
            chooseSeriesFragment = new ChooseSeriesFragment();
            chooseSeriesFragment.setMyCallBack(this);
        }

        chooseModelFragment = (ChooseModelFragment) fm.findFragmentByTag(ChooseModelFragment.class.getSimpleName());
        if (chooseModelFragment == null) {
            chooseModelFragment = new ChooseModelFragment();
            chooseModelFragment.setMyCallBack(this);
        }
        ft.commit();
    }

    @Override
    public void selectSeries(Series series) {
        super.selectSeries(series);
        String seriesId = series.getId();
        loadingDialog.show();
        AppApi.getCarModel(seriesId, this, 1, this);
    }

    @Override
    public void selectBrand(Brand brand) {
        super.selectBrand(brand);
        String brandId = brand.getId();
        loadingDialog.show();
        brandUrl = brand.getIcon();
        AppApi.getCarSeries(brandId, this, 0, this);
    }

    @Override
    public void selectModel(Model model) {
        super.selectModel(model);
        Intent intent = new Intent();
        intent.putExtra("model", model);
        intent.putExtra("url", brandUrl);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                SeriesListBean seriesListBean = JSON.parseObject(result, SeriesListBean.class);
                chooseSeriesFragment.setData(seriesListBean.getResultList(), brandUrl);
                chooseSeriesFragment.setMyCallBack(this);
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.container, chooseSeriesFragment);
                ft.addToBackStack("tag");
                ft.commit();
                break;
            case 1:

                JSONObject jsonObject = JSON.parseObject(result);
                String data = jsonObject.getString("returnObject");
                Map<String, List<Model>> stringListMap = (Map<String, List<Model>>) JSON.parse(data);
                Map<String, List<Model>> resultMap = sortMapByKey(stringListMap);
                chooseModelFragment.setData(resultMap);
                chooseModelFragment.setMyCallBack(this);
                FragmentManager fm1 = getSupportFragmentManager();
                FragmentTransaction ft1 = fm1.beginTransaction();
                ft1.add(R.id.container, chooseModelFragment);
                ft1.addToBackStack("tag");
                ft1.commit();
                break;
        }
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    /**
     * 使用 Map按key进行排序
     *
     * @param map
     * @return
     */
    private  Map<String, List<Model>> sortMapByKey(Map<String, List<Model>> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, List<Model>> sortMap = new TreeMap<String, List<Model>>(
                new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }

}



